package greate.project;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class MysqlDemo extends RouteBuilder {

    public static void main(String[] args) throws Exception {


        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://120.48.5.4:8806/data-exchange-zs");
        dataSource.setUser("root");
        dataSource.setPassword("baidu@123");
        CamelContext context = new DefaultCamelContext();
        context.getRegistry().bind("myDataSource", dataSource);

        try {
            context.addRoutes(new MysqlDemo());
        } catch (Exception e) {
            e.printStackTrace();
        }

        context.start();

        synchronized (HelloWorld.class) {
            try {
                HelloWorld.class.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void configure() throws Exception {


        from("timer://queryAward?period=60s")
                .setBody(constant(" SELECT * FROM acg_datasource;"))
                .to("jdbc:myDataSource?outputType=SelectList")
                .log("总体数据：${body[0].get('id')} == ${body[1].get('id')}")
                .split(body())
//                .log("${body.get('id')}")
                .choice()
                .when(simple("${body.get('name')} == '123'"))
//                .setBody(constant("INSERT INTO `data-exchange2`.`acg_datasource` (`id`, `name`, `remark`, `status`, `type`, `info_id`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('8', 'mysql11', 'mysql11', NULL, 'MYSQL1', 'ok', '1', '2021-06-08 19:26:49', NULL, NULL);"))
//                .to("jdbc:myDataSource")
                .log("constant : ${body}")
                .when(jsonpath("$.id").isEqualTo("1409759561797992450"))
                .log("jsonPath数据：${body}")
                .otherwise()
                .log("其他数据：${body}")
                .end();

    }

//    private static DataSource setupDataSource1(String connectURI1) {
//        DruidDataSource ds1 = new DruidDataSource();
//        ds1.setDriverClassName("com.mysql.jdbc.Driver");
//        ds1.setUsername("root");
//        ds1.setPassword("123456");
//        ds1.setUrl(connectURI1);
//        return ds1;
//    }

}
